package com.wms.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.TypePageQueryDTO;
import com.wms.entity.WmsGoods;
import com.wms.entity.WmsStorage;
import com.wms.entity.WmsType;
import com.wms.exception.GoodsUpdateException;
import com.wms.exception.StorageDeleteException;
import com.wms.exception.TypeDeleteException;
import com.wms.mapper.GoodsMapper;
import com.wms.mapper.TypeMapper;
import com.wms.result.PageResult;
import com.wms.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 分页查询
     * @param typePageQueryDTO
     * @return
     */
    @Override
    public PageResult page(TypePageQueryDTO typePageQueryDTO) {
        log.info("分页查询仓库信息:{}", typePageQueryDTO);
        PageHelper.startPage(typePageQueryDTO.getPage(), typePageQueryDTO.getPageSize());
        Page<WmsType> page = typeMapper.pageQuery(typePageQueryDTO);
        long total = page.getTotal();
        List<WmsType> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 新增
     * @param wmsType
     */
    @Override
    public void save(WmsType wmsType) {
        log.info("新增类型:{}", wmsType);
        typeMapper.save(wmsType);
    }

    /**
     * 修改
     * @param wmsType
     */
    @Override
    public void update(WmsType wmsType) {
        log.info("修改类型:{}", wmsType);
        typeMapper.update(wmsType);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        log.info("批量删除类型:{}", ids);
        for (Long id : ids) {
            List<WmsType> wmsTypes = goodsMapper.getByTypeId(id);
            // 若是存在物品则无法删除
            if (wmsTypes.size() > 0) {
                throw new TypeDeleteException("该分类已经关联了物品，无法删除");
            }
        }
        typeMapper.delete(ids);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public WmsType getById(Long id) {
        return typeMapper.getById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<WmsType> getAll() {
        return typeMapper.getAll();
    }

}
